#include "rrplane.h"

RRPlane::RRPlane()
{
    type = RR_PLANE;
}

void RRPlane::rrTrace(RRRay *ray, RRCrossInfo *arg)
{
    float denomi;
    RRVec3 d = ray->rrGetDir();
    RRVec3 r = ray->rrGetSrc();
    denomi = size.x * d.x + size.y * d.y + size.z * d.z;
    RRVec3 tosrc_temp, cross_temp;
    double s_temp, t_temp;
    if(denomi == 0) {
        arg->rrSetHit(false);
        return;
    }
    else {
        float numera;
        numera = size.x * (coord.x - r.x) + size.y * (coord.y - r.y) + size.z * (coord.z - r.z);
        float t;
        t = numera / denomi;
        if(t < 1.0e-4) {
            arg->rrSetHit(false);
            return;
        }
        else {
            t = t - 1.0e-2;
            cross_temp = RRSetVec3(r.x + t * d.x, r.y + t * d.y, r.z + t * d.z);
            tosrc_temp = RRSetVec3(-t * d.x, -t * d.y, -t * d.z);
            tosrc_temp = RRNormalize(tosrc_temp);
            s_temp = RRDistance(r, cross_temp);
            t_temp = s_temp + 1.0e-2;
            arg->rrSetHit(true);
        }
    }
    RRRange range_temp;
    range_temp.s = s_temp;
    range_temp.t = t_temp;
    RRVec3 norm_temp;
    norm_temp = RRSetVec3(size.x, size.y, size.z);
    norm_temp = RRNormalize(norm_temp);
    arg->rrSetCross(cross_temp);
    arg->rrSetNorm(norm_temp);
    arg->rrSetToSrc(tosrc_temp);
    arg->rrSetRange(range_temp);
    return;
}
